package com.zq.zhufengfm.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zq.zhufengfm.entity.Tracks;

import org.w3c.dom.Text;

import java.util.List;

import javax.xml.transform.Templates;

/**
 * Project_name : ZhuFengFM
 * Author : zhaoQiang
 * Date : 2015/10/24,19:28
 * Email : zhaoq_hero@163.com
 */
public class TrackItemAdapter extends BaseAdapter{

    private Context context;
    private List<Tracks> list;

    public TrackItemAdapter(Context context, List<Tracks> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if(list != null){
            ret = list.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View  ret = null;

        //视图复用：
        if(convertView!=null){
            ret = convertView;
        }else{

            ViewHolder holder = new ViewHolder();

            ret = LayoutInflater.from(context).inflate(R.layout.tracks_item,parent,false);

            holder.txt_title = (TextView) ret.findViewById(R.id.tracks_title);

            holder.playTimes = (TextView) ret.findViewById(R.id.track_playtimes);

            holder.imgView = (ImageView) ret.findViewById(R.id.track_img);

            ret.setTag(holder);
        }

        //
        ViewHolder viewHolder = (ViewHolder) ret.getTag();

        //填充数据：
        viewHolder.txt_title.setText(list.get(position).getTitle());
        viewHolder.playTimes.setText(list.get(position).getPlaytimes());

        //TODO: 暂时未设置
//        viewHolder.imgView.setImageResource();

        return ret;
    }

   private final class ViewHolder{
       public TextView txt_title;
       public ImageView imgView;
       public TextView playTimes;
    }
}
